#include "LineEditLWH.h"

#include <QLabel>
#include "qlineedit.h"
#include "qboxlayout.h"
#include "qregexp.h"
#include <QValidator>
#include <QEvent>
#include <QKeyEvent>

LineEditLWH::LineEditLWH(QWidget *parent) : QWidget(parent)
{
    bgColor = "#FFFFFF";
    borderColor = "#A6B5B8";
    borderRadius = 3;

    //用于显示星号的标签,居中对齐
    labAsterisk1 = new QLabel;
    labAsterisk1->setAlignment(Qt::AlignCenter);
    labAsterisk1->setText("*");

    labAsterisk2 = new QLabel;
    labAsterisk2->setAlignment(Qt::AlignCenter);
    labAsterisk2->setText("*");



    //用于输入长宽高的文本框,居中对齐
    txtProductL = new QLineEdit;
    txtProductL->setObjectName("txtProductL");
    txtProductL->setAlignment(Qt::AlignCenter);
    txtProductL->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    txtProductL->setPlaceholderText("长");
    connect(txtProductL, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));

    txtProductW = new QLineEdit;
    txtProductW->setObjectName("txtProductW");
    txtProductW->setAlignment(Qt::AlignCenter);
    txtProductW->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    txtProductW->setPlaceholderText("宽");
    connect(txtProductW, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));

    txtProductH = new QLineEdit;
    txtProductH->setObjectName("txtProductH");
    txtProductH->setAlignment(Qt::AlignCenter);
    txtProductH->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    txtProductH->setPlaceholderText("高");
    connect(txtProductH, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));


    // 限制输入框，输入的尺寸大小
    /*
    QIntValidator *validator_L = new QIntValidator(0, 1000, ui->lineEdit_L_set);
    ui->lineEdit_L_set->setValidator(validator_L);
    */

    //设置Size尺寸校验过滤
    QString pattern = "^\\d+\\*\\d+\\*\\d+$";// 正则表达式，匹配形如"L*H*G"的格式
    QRegExp regExp(pattern);
    QRegExpValidator *validator = new QRegExpValidator(regExp, this);

    txtProductL->setValidator(validator);
    txtProductW->setValidator(validator);
    txtProductH->setValidator(validator);

    //绑定事件过滤器,识别键盘按下
    txtProductL->installEventFilter(this);
    txtProductW->installEventFilter(this);
    txtProductH->installEventFilter(this);

    QFrame *frame = new QFrame;
    frame->setObjectName("frameProductSize");

    QStringList qss;
    qss.append("QWidget { font-family: Arial; font-size: 18px; }"); // 添加这行来设置全局字体
    qss.append(QString("QFrame#frameIP{border:1px solid %1;border-radius:%2px;}").arg(borderColor).arg(borderRadius));
    qss.append(QString("QLabel{min-width:15px;background-color:%1;}").arg(bgColor));
    qss.append(QString("QLineEdit{background-color:%1;border:none;}").arg(bgColor));
    qss.append(QString("QLineEdit#txtIP1{border-top-left-radius:%1px;border-bottom-left-radius:%1px;}").arg(borderRadius));
    qss.append(QString("QLineEdit#txtIP4{border-top-right-radius:%1px;border-bottom-right-radius:%1px;}").arg(borderRadius));
    frame->setStyleSheet(qss.join(""));

    QVBoxLayout *verticalLayout = new QVBoxLayout(this);
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    verticalLayout->setSpacing(0);
    verticalLayout->addWidget(frame);

    //将控件按照横向布局排列
    QHBoxLayout *layout = new QHBoxLayout(frame);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->addWidget(txtProductL);
    layout->addWidget(labAsterisk1);
    layout->addWidget(txtProductW);
    layout->addWidget(labAsterisk2);
    layout->addWidget(txtProductH);

}



bool LineEditLWH::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QLineEdit *txt = (QLineEdit *)watched;
        if (txt == txtProductL || txt == txtProductW || txt == txtProductH) {
            QKeyEvent *key = (QKeyEvent *)event;

            //如果当前按下了小数点则移动焦点到下一个输入框
            if (key->text() == "*") {
                this->focusNextChild();
            }

            // 如果是第一个输入框，不移动焦点，只执行退格删除操作
            if (watched == txtProductL) {
                return false; // 继续执行默认的退格操作
            }

            //如果按下了退格键并且当前文本框已经没有了内容则焦点往前移
            if (key->key() == Qt::Key_Backspace) {
                if (txt->text().length() <= 1) {
                    this->focusNextPrevChild(false);
                }
            }
        }
    }

    return QWidget::eventFilter(watched, event);
}

void LineEditLWH::textChanged(const QString &text)
{
    int len = text.length();
    int value = text.toInt();

    //判断当前是否输入完成一个网段,是的话则自动移动到下一个输入框
    if (len == 3) {
        if (value >= 100 && value <= 999) {
            this->focusNextChild();
        }
    }

    //拼接成完整IP地址
    size = QString("%1*%2*%3").arg(txtProductL->text()).arg(txtProductW->text()).arg(txtProductH->text());

    emit sizeChange(size);
}

QString LineEditLWH::getSize() const
{
    return this->size;
}

QSize LineEditLWH::sizeHint() const
{
    return QSize(250, 20);
}

QSize LineEditLWH::minimumSizeHint() const
{
    return QSize(30, 10);
}

void LineEditLWH::setSize(const QString &size)
{
    //先检测尺寸大小是否合法
    QRegExp regExp("^\\d+\\*\\d+\\*\\d+$");// 正则表达式，匹配形如"L*H*G"的格式
    if (!regExp.exactMatch(size)) {
        return;
    }

    if (this->size != size) {
        this->size = size;

        //将尺寸大小填入各个网段
        QStringList list = size.split("*");
        txtProductL->setText(list.at(0));
        txtProductW->setText(list.at(1));
        txtProductH->setText(list.at(2));
    }
}

void LineEditLWH::clear()
{
    txtProductL->clear();
    txtProductW->clear();
    txtProductH->clear();
    txtProductL->setFocus();
}


void LineEditLWH::setBgColor(const QString &bgColor)
{
    if (this->bgColor != bgColor) {
        this->bgColor = bgColor;
    }
}

void LineEditLWH::setBorderColor(const QString &borderColor)
{
    if (this->borderColor != borderColor) {
        this->borderColor = borderColor;
    }
}

void LineEditLWH::setBorderRadius(int borderRadius)
{
    if (this->borderRadius != borderRadius) {
        this->borderRadius = borderRadius;
    }
}





